package com.jit.monitor.ui.activity.user;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.jit.baselib.ui.base.BaseActivity;
import com.jit.baselib.util.ui.StatusBarHelper;
import com.jit.baselib.util.ui.UIUtils;
import com.jit.monitor.R;
import com.jit.monitor.presenter.user.RgAtPresenter;
import com.jit.monitor.view.RgAtView;

import butterknife.BindView;

/**
 * @author crazyZhangxl on 2018-12-11 14:34:41.
 * Describe:
 */

public class RegisterActivity extends BaseActivity<RgAtView,RgAtPresenter> implements RgAtView {
    @BindView(R.id.ivAccountDel)
    ImageView mIvAccountDel;
    @BindView(R.id.etAccount)
    EditText mEtAccount;
    @BindView(R.id.ivPwdDel)
    ImageView mIvPwdDel;
    @BindView(R.id.etPwd)
    EditText mEtPwd;
    @BindView(R.id.btnRegister)
    Button mBtnRegister;
    @Override
    protected void init() {

    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_register;
    }

    @Override
    protected RgAtPresenter createPresenter() {
        return new RgAtPresenter(this);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        StatusBarHelper.setStatusBarLightMode(this);

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {


        mEtAccount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                boolean isAccountNull = mEtAccount.getText().toString().trim().length() != 0;
                boolean isPwdNull = mEtPwd.getText().toString().trim().length() != 0;
                mBtnRegister.setEnabled(isAccountNull && isPwdNull);
                if (isAccountNull){
                    mIvAccountDel.setVisibility(View.VISIBLE);
                }else {
                    mIvAccountDel.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mEtAccount.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus){
                if (mEtAccount.getText().toString().trim().length() != 0){
                    mIvAccountDel.setVisibility(View.VISIBLE);
                }
            }else {
                mIvAccountDel.setVisibility(View.GONE);
            }
        });

        mEtPwd.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus){
                if (mEtPwd.getText().toString().trim().length() != 0){
                    mIvPwdDel.setVisibility(View.VISIBLE);
                }else {
                    mIvPwdDel.setVisibility(View.GONE);
                }
            }else {
                mIvPwdDel.setVisibility(View.GONE);
            }
        });
        mEtPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                boolean isAccountNull = mEtAccount.getText().toString().trim().length() != 0;
                boolean isPwdNull = mEtPwd.getText().toString().trim().length() != 0;
                mBtnRegister.setEnabled(isAccountNull && isPwdNull);
                if (isPwdNull){
                    mIvPwdDel.setVisibility(View.VISIBLE);
                }else {
                    mIvPwdDel.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        mIvAccountDel.setOnClickListener(v -> {
            mEtAccount.setText("");
            mBtnRegister.setEnabled(false);
        });

        mIvPwdDel.setOnClickListener(v -> {
            mEtPwd.setText("");
            mBtnRegister.setEnabled(false);
        });


        mBtnRegister.setOnClickListener(v -> {
            String account = mEtAccount.getText().toString();
            String pwd = mEtPwd.getText().toString();
            mPresenter.doRegister(account, pwd);
        });
    }

    @Override
    public void registerSuccess() {
        finish();
       jumpToActivity(LoginActivity.class);
    }

    @Override
    public void registerFailure(String error) {
        UIUtils.showToast(error);
        mBtnRegister.setEnabled(false);
    }
}
